<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class authen extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
                        $this->login();
	}
        
                public function login(){
                    //authen
                    if(!isset($_SESSION["username"])){
                        $this->load->view("authen/loginpage");
                    }else{
                        @header( 'Location: ' . base_url() . "activity/view_activitys") ;
                    }
                }
                
                public function logout(){
                    session_start();
                    unset($_SESSION["username"] , $_SESSION["password"] , $_SESSION["account_type"]);
                    @header( 'Location: ' . base_url() . "authen/login/_file/login.go") ;
                }
                
                public function checklogin(){
                    session_start();
                    $username = mysql_escape_string($_POST["username"]);
                    $password  = mysql_escape_string($_POST["password"]);
                    $account_type = mysql_escape_string($_POST["account_type"]);
                    
                    switch ($account_type){
                        case "class_a":
                            $login = $this->authen_model->check_user_db( $username , $password , "class_a");
                            if($login->num_rows() == 1){
                                    $_SESSION["username"] = $login->row()->admin_username;
                                    $_SESSION["id"] = $login->row()->admin_id;
                                    $_SESSION["type"] = "class_a";
                                    $message_welcome = "ยินดีต้อนรับผู้ดูแลระบบเข้าสู่ระบบ";
                                    $login_success = true;
                            }else{
                                    $login = null;
                                    $login_success = false;
                            }
                            break;
                        case "class_t":
                            $login = $this->authen_model->check_user_db( $username , $password , "class_t");
                            if($login->num_rows() == 1){
                                    $_SESSION["username"] = $login->row()->teacher_name;
                                    $_SESSION["id"] = $login->row()->teacher_id;
                                    $_SESSION["type"] = "class_t";
                                    $message_welcome = "ยินดีต้อนรับบุคลากรอาจารย์เข้าสู่ระบบ";
                                    $login_success = true;
                            }else{
                                    $login = null;
                                    $login_success = false;
                            }
                            break;
                        case "class_c":
                            $login = $this->authen_model->check_user_db( $username , $password , "class_c");
                            if($login->num_rows() == 1){
                                    $_SESSION["username"] = $login->row()->collegian_name;
                                    $_SESSION["id"] = $login->row()->collegian_id;
                                    $_SESSION["collegian_code"] = $login->row()->collegian_code;
                                    $_SESSION["type"] = "class_c";
                                    $message_welcome = "ยินดีต้อนรับนักศึกษาเข้าสู่ระบบ";
                                    $login_success = true;
                            }else{
                                    $login = null;
                                    $login_success = false;
                            }
                            break;
                        default :
                            $login = null;
                            $login_success = false;
                    }
                    
                    if( $login != null  &&  $login_success != false){

                        print('
                            <script>
                                          alert("'  .  $message_welcome  . '");
                                         top.gotoPage();
                            </script>
                            ');
                    }else{
                        print('<script>alert("ชื่อเข้าสู่ระบบไม่ถูกต้องกรุณาตรวจสอบ")</script>');
                        print('<script> top.resetlogin(); </script>');
                    }
                    //$this->db->select("*")->from("account")->where("");
                }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */